/*
 * Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* ScriptData
SDName: Sholazar_Basin
SD%Complete: 100
SDComment: Quest support: 11253, 11241.
SDCategory: howling_fjord
EndScriptData */

/* ContentData
npc_plaguehound_tracker
npc_apothecary_hanes
EndContentData */

#include "ScriptPCH.h"
#include "ScriptedEscortAI.h"

/*######
## npc_apothecary_hanes
######*/
enum Entries
{
    NPC_APOTHECARY_HANES         = 23784,
    FACTION_ESCORTEE_A           = 774,
    FACTION_ESCORTEE_H           = 775,
    NPC_HANES_FIRE_TRIGGER       = 23968,
    QUEST_TRAIL_OF_FIRE          = 11241,
    SPELL_COSMETIC_LOW_POLY_FIRE = 56274
};

class npc_apothecary_hanes : public CreatureScript
{
public:
    npc_apothecary_hanes() : CreatureScript("npc_apothecary_hanes") { }

    bool OnQuestAccept(Player* pPlayer, Creature* pCreature, Quest const* quest)
    {
        if (quest->GetQuestId() == QUEST_TRAIL_OF_FIRE)
        {
            switch (pPlayer->GetTeam())
            {
                case ALLIANCE:
                    pCreature->setFaction(FACTION_ESCORTEE_A);
                    break;
                case HORDE:
                    pCreature->setFaction(FACTION_ESCORTEE_H);
                    break;
            }
            CAST_AI(npc_escortAI, (pCreature->AI()))->Start(true, false, pPlayer->GetGUID());
        }
        return true;
    }

    struct npc_Apothecary_HanesAI : public npc_escortAI
    {
        npc_Apothecary_HanesAI(Creature* pCreature) : npc_escortAI(pCreature){}
        uint32 PotTimer;

        void Reset ()
        {
            SetDespawnAtFar(false);
            PotTimer = 10000; //10 sec cooldown on potion
        }

        void JustDied(Unit* /*killer*/)
        {
            if (Player* pPlayer = GetPlayerForEscort())
                pPlayer->FailQuest(QUEST_TRAIL_OF_FIRE);
        }

        void UpdateEscortAI(const uint32 diff)
        {
            if (HealthBelowPct(75))
            {
                if (PotTimer <= diff)
                {
                    DoCast(me, 17534, true);
                    PotTimer = 10000;
                } else PotTimer -= diff;
            }
            if (GetAttack() && UpdateVictim())
                DoMeleeAttackIfReady();
        }

        void WaypointReached(uint32 i)
        {
            Player* pPlayer = GetPlayerForEscort();
            if (!pPlayer)
                return;
            switch(i)
            {
                case 1:
                    me->SetReactState(REACT_AGGRESSIVE);
                    SetRun(true);
                    break;
                case 23:
                    if (pPlayer)
                        pPlayer->GroupEventHappens(QUEST_TRAIL_OF_FIRE, me);
                    me->ForcedDespawn();
                    break;
                case 5:
                    if (Unit* Trigger = me->FindNearestCreature(NPC_HANES_FIRE_TRIGGER,10.0f))
                        Trigger->CastSpell(Trigger, SPELL_COSMETIC_LOW_POLY_FIRE, false);
                    SetRun(false);
                    break;
                case 6:
                    if (Unit* Trigger = me->FindNearestCreature(NPC_HANES_FIRE_TRIGGER,10.0f))
                        Trigger->CastSpell(Trigger, SPELL_COSMETIC_LOW_POLY_FIRE, false);
                    SetRun(true);
                    break;
                case 8:
                    if (Unit* Trigger = me->FindNearestCreature(NPC_HANES_FIRE_TRIGGER,10.0f))
                        Trigger->CastSpell(Trigger, SPELL_COSMETIC_LOW_POLY_FIRE, false);
                    SetRun(false);
                    break;
                case 9:
                    if (Unit* Trigger = me->FindNearestCreature(NPC_HANES_FIRE_TRIGGER,10.0f))
                        Trigger->CastSpell(Trigger, SPELL_COSMETIC_LOW_POLY_FIRE, false);
                    break;
                case 10:
                    SetRun(true);
                    break;
                case 13:
                    SetRun(false);
                    break;
                case 14:
                    if (Unit* Trigger = me->FindNearestCreature(NPC_HANES_FIRE_TRIGGER,10.0f))
                        Trigger->CastSpell(Trigger, SPELL_COSMETIC_LOW_POLY_FIRE, false);
                    SetRun(true);
                    break;
            }
        }
    };

    CreatureAI *GetAI(Creature *creature) const
    {
        return new npc_Apothecary_HanesAI(creature);
    }
};

/*######
## npc_plaguehound_tracker
######*/

enum ePlaguehound
{
    QUEST_SNIFF_OUT_ENEMY        = 11253
};

class npc_plaguehound_tracker : public CreatureScript
{
public:
    npc_plaguehound_tracker() : CreatureScript("npc_plaguehound_tracker") { }

    struct npc_plaguehound_trackerAI : public npc_escortAI
    {
        npc_plaguehound_trackerAI(Creature* pCreature) : npc_escortAI(pCreature) { }

        void Reset()
        {
            InitScriptData();
        }

        void InitScriptData()
        {
            Player* pPlayer = NULL;
            if (me->isSummon())
                if (Unit* summoner = CAST_SUM(me)->GetSummoner())
                    if (summoner->GetTypeId() == TYPEID_PLAYER)
                        pPlayer = CAST_PLR(summoner);

            if (!pPlayer)
                return;

            me->SetUnitMovementFlags(MOVEMENTFLAG_WALKING);
            Start(false, false, pPlayer->GetGUID());
        }

        void WaypointReached(uint32 i)
        {
            Player* pPlayer = NULL;
            if (me->isSummon())
                if (Unit* summoner = CAST_SUM(me)->GetSummoner())
                    if (summoner->GetTypeId() == TYPEID_PLAYER)
                        pPlayer = CAST_PLR(summoner);

            if (!pPlayer)
                return;

            switch(i)
            {
            case 26:
                me->ForcedDespawn();
                break;
            }
        }
    };

    CreatureAI *GetAI(Creature *creature) const
    {
        return new npc_plaguehound_trackerAI(creature);
    }
};

/*######
## npc_razael_and_lyana
######*/

#define GOSSIP_RAZAEL_REPORT "High Executor Anselm wants a report on the situation."
#define GOSSIP_LYANA_REPORT "High Executor Anselm requests your report."

enum eRazael
{
    QUEST_REPORTS_FROM_THE_FIELD = 11221,
    NPC_RAZAEL = 23998,
    NPC_LYANA = 23778,
    GOSSIP_TEXTID_RAZAEL1 = 11562,
    GOSSIP_TEXTID_RAZAEL2 = 11564,
    GOSSIP_TEXTID_LYANA1 = 11586,
    GOSSIP_TEXTID_LYANA2 = 11588
};

class npc_razael_and_lyana : public CreatureScript
{
public:
    npc_razael_and_lyana() : CreatureScript("npc_razael_and_lyana") { }

    bool OnGossipHello(Player* pPlayer, Creature* pCreature)
    {
        if (pCreature->isQuestGiver())
            pPlayer->PrepareQuestMenu(pCreature->GetGUID());

        if (pPlayer->GetQuestStatus(QUEST_REPORTS_FROM_THE_FIELD) == QUEST_STATUS_INCOMPLETE)
            switch (pCreature->GetEntry())
            {
                case NPC_RAZAEL:
                    if (!pPlayer->GetReqKillOrCastCurrentCount(QUEST_REPORTS_FROM_THE_FIELD, NPC_RAZAEL))
                    {
                        pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_RAZAEL_REPORT, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 1);
                        pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_RAZAEL1, pCreature->GetGUID());
                        return true;
                    }
                break;
                case NPC_LYANA:
                    if (!pPlayer->GetReqKillOrCastCurrentCount(QUEST_REPORTS_FROM_THE_FIELD, NPC_LYANA))
                    {
                        pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_LYANA_REPORT, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 2);
                        pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_LYANA1, pCreature->GetGUID());
                        return true;
                    }
                break;
            }
        pPlayer->SEND_GOSSIP_MENU(pPlayer->GetGossipTextId(pCreature), pCreature->GetGUID());
        return true;
    }

    bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 /*uiSender*/, uint32 uiAction)
    {
        pPlayer->PlayerTalkClass->ClearMenus();
        switch (uiAction)
        {
            case GOSSIP_ACTION_INFO_DEF + 1:
                pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_RAZAEL2, pCreature->GetGUID());
                pPlayer->TalkedToCreature(NPC_RAZAEL, pCreature->GetGUID());
                break;
            case GOSSIP_ACTION_INFO_DEF + 2:
                pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_LYANA2, pCreature->GetGUID());
                pPlayer->TalkedToCreature(NPC_LYANA, pCreature->GetGUID());
                break;
        }
        return true;
    }
};

/*######
## npc_mcgoyver
######*/

#define GOSSIP_ITEM_MG_I  "Walt sent me to pick up some dark iron ingots."
#define GOSSIP_ITEM_MG_II "Yarp."

enum eMcGoyver
{
    QUEST_WE_CAN_REBUILD_IT             = 11483,

    SPELL_CREATURE_DARK_IRON_INGOTS     = 44512,
    SPELL_TAXI_EXPLORERS_LEAGUE         = 44280,

    GOSSIP_TEXTID_MCGOYVER              = 12193
};

class npc_mcgoyver : public CreatureScript
{
public:
    npc_mcgoyver() : CreatureScript("npc_mcgoyver") { }

    bool OnGossipHello(Player* pPlayer, Creature* pCreature)
    {
        if (pPlayer->GetQuestStatus(QUEST_WE_CAN_REBUILD_IT) == QUEST_STATUS_INCOMPLETE)
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_ITEM_MG_I, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1);

        pPlayer->SEND_GOSSIP_MENU(pPlayer->GetGossipTextId(pCreature), pCreature->GetGUID());
        return true;
    }

    bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 /*uiSender*/, uint32 uiAction)
    {
        pPlayer->PlayerTalkClass->ClearMenus();
        switch(uiAction)
        {
            case GOSSIP_ACTION_INFO_DEF+1:
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_ITEM_MG_II, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+2);
                pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_MCGOYVER, pCreature->GetGUID());
                pPlayer->CastSpell(pPlayer, SPELL_CREATURE_DARK_IRON_INGOTS, true);
                break;
            case GOSSIP_ACTION_INFO_DEF+2:
                pPlayer->CastSpell(pPlayer, SPELL_TAXI_EXPLORERS_LEAGUE, true);
                pPlayer->CLOSE_GOSSIP_MENU();
                break;
        }
        return true;
    }
};

/*######
## Drop It then Rock It
######*/

enum eAlliance
{
    NPC_WINTERSKORN_DEFENDER	     = 24015,
    QUEST_DROP_IT_THEN_ROCK_IT	     = 11429
};

struct npc_alliance_banner_standardAI : public ScriptedAI
{
    npc_alliance_banner_standardAI(Creature *c) : ScriptedAI(c), lSummons(me)
	{
	summonNumber = 0 ;
	summonTimer = 2000 ;
	}

	uint32 summonTimer ;
	uint8 summonNumber ;
	SummonList lSummons;

    void Reset()
    {
	me->SetReactState(REACT_PASSIVE) ;
    }

	void JustSummoned(Creature* summon)
	{
		lSummons.Summon(summon);
	}

	void JustDied(Unit *who)
	{
		lSummons.DespawnAll();
	}

    void EnterCombat(Unit *who) {}

	void UpdateAI(const uint32 diff)
	{
		if(summonTimer <= diff)
		{
			if(summonNumber >= 3)
			{
				if(Unit* owner = me->GetOwner())
				{
					if(owner->GetTypeId() == TYPEID_PLAYER)
					{
						Player* player = CAST_PLR(owner);
						player->GroupEventHappens(QUEST_DROP_IT_THEN_ROCK_IT, me) ;
					}
				}
				me->DisappearAndDie() ;
			}
			uint8 summons ;
			if(summonNumber<= 3)
				summons = 1 ;
			else if(summonNumber<= 4)
				summons = 2 ;
			else summons = 3 ;
			for(int i = 0 ; i<summons ; i++)
			{
				Creature* defender = me->SummonCreature(NPC_WINTERSKORN_DEFENDER, me->GetPositionX()+20, me->GetPositionY()+20, me->GetPositionZ()) ;
				if(defender)
				{
					defender->AI()->AttackStart(me) ;
				}
			}
			summonNumber ++ ;
			summonTimer = 16000 ;
		} else summonTimer -= diff ; 

	if (!UpdateVictim())
            return;
     }
};

 /*######
## npc_moonleaf_and_mistwalker
######*/

#define GOSSIP_ITEM_DISGUISE    "I have misplaced my worg disguise."

enum
{
    SPELL_WORG_DISGUISE         = 43379,
    ITEM_WORG_DISGUISE          = 33618,
    QUEST_WORGS_CLOTHING_A      = 11325,
    QUEST_BROTHER_BETRAYERS_A   = 11414,
    QUEST_EYES_OF_THE_EAGLE_A   = 11416,
    QUEST_ALPHA_WORG_A          = 11326,
    QUEST_WORGS_CLOTHING_H      = 11323,
    QUEST_BROTHER_BETRAYERS_H   = 11415,
    QUEST_EYES_OF_THE_EAGLE_H   = 11417,
    QUEST_ALPHA_WORG_H          = 11324,    
    GOSSIP_TEXTID_LOOSE         = 11870
};
class npc_moonleaf_and_mistwalker : public CreatureScript
{
public:
    npc_moonleaf_and_mistwalker() : CreatureScript("npc_moonleaf_and_mistwalker") { }

    bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 uiSender, uint32 uiAction)
    {
        switch(uiAction)
        {
            case GOSSIP_ACTION_INFO_DEF:
                pPlayer->CastSpell(pPlayer,SPELL_WORG_DISGUISE,false);
                pPlayer->SEND_GOSSIP_MENU(GOSSIP_TEXTID_LOOSE, pCreature->GetGUID());
        }
    
        return true;
    }


    bool OnGossipHello(Player* pPlayer, Creature* pCreature)
    {
        if (pCreature->isQuestGiver())
            pPlayer->PrepareQuestMenu(pCreature->GetGUID());
    
        if ((pPlayer->GetQuestStatus(QUEST_WORGS_CLOTHING_A) == QUEST_STATUS_COMPLETE || 
            pPlayer->GetQuestStatus(QUEST_BROTHER_BETRAYERS_A) == QUEST_STATUS_INCOMPLETE || 
            pPlayer->GetQuestStatus(QUEST_EYES_OF_THE_EAGLE_A) == QUEST_STATUS_INCOMPLETE) && 
            !pPlayer->GetQuestRewardStatus(QUEST_ALPHA_WORG_A) &&
            !pPlayer->HasItemCount(ITEM_WORG_DISGUISE,1,true))
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_ITEM_DISGUISE, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);

        if ((pPlayer->GetQuestStatus(QUEST_WORGS_CLOTHING_H) == QUEST_STATUS_COMPLETE ||
            pPlayer->GetQuestStatus(QUEST_BROTHER_BETRAYERS_H) == QUEST_STATUS_INCOMPLETE ||
            pPlayer->GetQuestStatus(QUEST_EYES_OF_THE_EAGLE_H) == QUEST_STATUS_INCOMPLETE) &&
            !pPlayer->GetQuestRewardStatus(QUEST_ALPHA_WORG_H) &&
            !pPlayer->HasItemCount(ITEM_WORG_DISGUISE,1,true))
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_ITEM_DISGUISE, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);

        pPlayer->SEND_GOSSIP_MENU(pPlayer->GetGossipTextId(pCreature), pCreature->GetGUID());
        return true;
    }


};
class npc_alliance_banner : public CreatureScript
{
public:
    npc_alliance_banner() : CreatureScript("npc_alliance_banner") { }

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new npc_alliance_banner_standardAI (pCreature);
    }


};
void AddSC_howling_fjord()
{
    new npc_apothecary_hanes;
    new npc_plaguehound_tracker;
    new npc_razael_and_lyana;
    new npc_mcgoyver; 
    new npc_alliance_banner();
    new npc_moonleaf_and_mistwalker();
 }
